package com.mikgonz.farkle.ui;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

import com.mikgonz.farkle.R;

public class PlayerSetup extends PreferenceActivity {

	private int numOfPlayers = 0;
	private boolean playersPrefChanged = false;
	private Resources r;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.local_players_prefs);
		r = getResources();

		ListPreference l = (ListPreference) findPreference(r
				.getString(R.string.PREF_number_of_players));

		if (savedInstanceState != null
				&& savedInstanceState.getBoolean("playersPrefChanged")) {
			numOfPlayers = Integer.valueOf(l.getValue());
			enablePrefs();
			setUpListeners(false);

		}

		l.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {

				playersPrefChanged = true;

				numOfPlayers = Integer.valueOf((String) newValue);

				enablePrefs();
				setUpListeners(true);

				return true;
			}
		});

		Preference p = findPreference("playPref");

		p.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				startActivity(new Intent(PlayerSetup.this, GameScreen.class));
				PlayerSetup.this.finish();
				return true;
			}

		});

	}

	protected void enablePrefs() {

		Preference p = null;
		for (int i = 1; i <= 4; i++) {

			p = (Preference) findPreference("player" + i + "Pref");

			if (i <= numOfPlayers) {

				p.setEnabled(true);
			} else {
				p.setEnabled(false);
			}
		}

		p = findPreference("playPref");
		p.setEnabled(true);
	}

	protected void setUpListeners(boolean setListeners) {
		CheckBoxPreference c = null;

		for (int i = 2; i <= numOfPlayers; i++) {
			c = (CheckBoxPreference) findPreference("player" + i
					+ "IsHumanPref");

			EditTextPreference e = (EditTextPreference) findPreference("player"
					+ i + "NamePref");
			ListPreference l = (ListPreference) findPreference("player" + i
					+ "DiffPref");

			if (c.isChecked() == true) {
				e.setEnabled(true);
				l.setEnabled(false);
			} else {
				e.setEnabled(false);
				l.setEnabled(true);
			}

			if (setListeners) {

				c.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {

						int i = Integer.valueOf(""
								+ preference.getKey().charAt(6));

						EditTextPreference e = (EditTextPreference) findPreference("player"
								+ i + "NamePref");
						ListPreference l = (ListPreference) findPreference("player"
								+ i + "DiffPref");

						if (((Boolean) newValue).booleanValue() == true) {
							e.setEnabled(true);
							l.setEnabled(false);
						} else {
							e.setEnabled(false);
							l.setEnabled(true);
						}

						return true;
					}

				});
			}

		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		outState.putBoolean("playersPrefChanged", playersPrefChanged);
		super.onSaveInstanceState(outState);
	}
}
